import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class MyStackUsedQueue {
    public Queue<Integer> queue1;
    public Queue<Integer> queue2;
    public MyStackUsedQueue() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    public void push(int x) {
        if(queue1.isEmpty()){
            queue2.offer(x);
        }else{
            queue1.offer(x);
        }
    }

    public int pop() {
        if(empty()){
            return -1;
        }
        if(!queue1.isEmpty()){
            int size = queue1.size();
            for (int i = 0; i < size - 1; i++) {
                queue2.offer(queue1.poll());
            }
            int ret = queue1.poll();
            return ret;
        }else{
            int size = queue2.size();
            for (int i = 0; i < size - 1; i++) {
                queue1.offer(queue2.poll());
            }
            int ret = queue2.poll();
            return ret;
        }
    }

    public int top() {
        if(empty()){
            return -1;
        }
        if(!queue1.isEmpty()){
            int size = queue1.size();
            for (int i = 0; i < size - 1; i++) {
                queue2.offer(queue1.poll());
            }
            int ret = queue1.poll();
            queue2.offer(ret);
            return ret;
        }else{
            int size = queue2.size();
            for (int i = 0; i < size - 1; i++) {
                queue1.offer(queue2.poll());
            }
            int ret = queue2.poll();
            queue1.offer(ret);
            return ret;
        }
    }

    public boolean empty() {
        return queue1.isEmpty()&&queue2.isEmpty();
    }
}
